package com.leizhi.demo.monitor.config;

import com.leizhi.demo.monitor.achieve.PushStreamService;
import com.leizhi.demo.monitor.achieve.RtpService;
import com.leizhi.demo.monitor.achieve.RtpUsablePort;
import com.leizhi.demo.monitor.achieve.SipService;
import com.leizhi.demo.monitor.pojo.SipDispChange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description 监控配置
 * @Version 1.0.0-SNAPSHOT
 * @Author leizhi
 * @Date 2025/4/27 9:45
 */
@Configuration
public class MonitorConfig {

    private static final Logger logger = LoggerFactory.getLogger(MonitorConfig.class);


    @Bean
    @ConfigurationProperties(prefix = "sip")
    public SipDispChange sipDispChange() {
        return new SipDispChange();
    }

    @Bean
    public RtpUsablePort rtpUsablePort(SipDispChange sipDispChange) {
        return new RtpUsablePort(sipDispChange.getUdpRtpPorts());
    }

    @Bean
    public PushStreamService pushStreamService(SipDispChange sipDispChange) {
        PushStreamService pushStreamService = new PushStreamService();
        RtpService.setInstance(pushStreamService, sipDispChange.getTcpRtpPort(), sipDispChange.getPushRtmpAddress());
        return pushStreamService;
    }

    @Bean
    public SipService sipService(SipDispChange sipDispChange) {
        SipService sipService = new SipService();
        if (sipService.initSip(sipDispChange)) {
            logger.info("SIP 服务启动成功，端口号：{}", sipDispChange.getSipPort());
        } else {
            throw new RuntimeException("SIP 服务启动失败");
        }
        return sipService;
    }

}
